// import parseTime, formatTime and set to filter
export { parseTime, formatTime }
from '@/utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
    if (time === 1) {
        return time + label
    }
    return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
    const between = Date.now() / 1000 - Number(time)
    if (between < 3600) {
        return pluralize(~~(between / 60), ' minute')
    } else if (between < 86400) {
        return pluralize(~~(between / 3600), ' hour')
    } else {
        return pluralize(~~(between / 86400), ' day')
    }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
    const si = [
        { value: 1E18, symbol: 'E' },
        { value: 1E15, symbol: 'P' },
        { value: 1E12, symbol: 'T' },
        { value: 1E9, symbol: 'G' },
        { value: 1E6, symbol: 'M' },
        { value: 1E3, symbol: 'k' }
    ]
    for (let i = 0; i < si.length; i++) {
        if (num >= si[i].value) {
            return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
        }
    }
    return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
    return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
    return string.charAt(0).toUpperCase() + string.slice(1)
}

//审批状态
export function statusFilter(val) {
    switch (val) {
        case 1:
            return '待审批';
            break;
        case 2:
            return '待转账';
            break;
        case 3:
            return '已拒绝';
            break;
        case 4:
            return '已完成';
            break;
        default:
            break;
    }
}

//订单状态filter
export function orderFilter(value) {
    switch (value) {
        case 0:
            return '待支付'
            break;
        case 10:
            return '支付成功'
            break;
        case 20:
            return '待收货'
            break;
        case 30:
            return '确认收货'
            break;
        case 40:
            return '订单完结'
            break;
        case 50:
            return '超时支付'
            break;
        default:
            break;
    }
}

export function orderType(type) {
    switch (type) {
        case 'presell':
            return '工厂预售'
            break;
        case 'consign':
            return '寄售'
            break;
        case 'self_support':
            return '平台自营'
            break;
        case 'wish':
            return '心愿单'
            break;
        default:
            break;
    }
}